# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/13
# python-version    : Python 3.7.0
# description       : 定义聊天室基类

from com.python.demo.mutil_chatting_room.entities.CommandHandler import CommandHandler
from com.python.demo.mutil_chatting_room.entities.CloseSession import CloseSession


class Room(CommandHandler):
    "包含多个用户的环境，负责基本的命令处理和广播"

    def __init__(self, server):
        self.server = server
        self.sessions = []

    def add(self, session):
        "用户进入房间"
        self.sessions.append(session)

    def remove(self, session):
        "用户离开房间"
        self.sessions.remove(session)

    def broadcast(self, line):
        "群发消息"
        for session in self.sessions:
            session.push(line)

    def do_logout(self, session, line):
        "用户登出"
        raise CloseSession
